package com.l.d.app.library.app;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.support.v4.app.DialogFragment;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import com.l.d.app.library.util.WindowUtil;

/**
 * Created by linde on 2017/4/18.
 * <p/>
 */

public abstract class NoTitleDialogFragment extends DialogFragment {

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, style());
    }

    @Nullable
    @Override
    public final View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(layout(), container, false);
    }

    @Override
    public final void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        final Context context = view.getContext();

        final DisplayMetrics dm = WindowUtil.displayMetrics(context);
        final int minimumWidth = minimumWidth(dm);
        final int minimumHeight = minimumHeight(dm);
        if (minimumWidth > 0) {
            view.setMinimumWidth(minimumWidth);
        }
        if (minimumHeight > 0) {
            view.setMinimumHeight(minimumHeight);
        }

        Window window = getDialog().getWindow();
        if (window != null) {
            window.setWindowAnimations(style());
            window.setGravity(gravity());
        }
        initView(view, savedInstanceState);
    }

    @StyleRes
    protected abstract int style();

    @LayoutRes
    protected abstract int layout();

    protected abstract int gravity();

    protected abstract int minimumWidth(@NonNull DisplayMetrics dm);

    protected abstract int minimumHeight(@NonNull DisplayMetrics dm);

    protected abstract void initView(View view, Bundle savedInstanceState);
}
